/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package RestaurantManagement;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Restaurant billing implementation that handles 
 * customer orders and bill calculation
 * @author ashongtical
 */

public class RestaurantBilling extends RestaurantBillingSystem {
    private ArrayList<Meal> order;
    private HashMap<String, Integer> mealQuantities;

    // Constructor to initialize order list and meal quantities map
    public RestaurantBilling() {
        super();
        this.order = new ArrayList<>();
        this.mealQuantities = new HashMap<>();
    }

    // Add a meal from the menu to the customer's order
    // mealName: The name of the meal to add
    // return true if meal was added successfully, false otherwise
    @Override
    public boolean addMealToOrder(String mealName){
        Meal meal = findMealByName(mealName);
        if(meal != null){
            order.add(meal);
            if(mealQuantities.containsKey(mealName)){
                int currentQuantity = mealQuantities.get(mealName);
                mealQuantities.put(mealName, currentQuantity + 1);
            }else {
                mealQuantities.put(mealName, 1);
            }
            System.out.println("Added '" + mealName + "' to the order.");
            return true;
        }
        System.out.println("Meal '" + mealName + "' not found on the menu.");
        return false;
    }

    // Override the calculateBill() and calculate the total bill for the customer's order
    // return the total bill amount
    @Override
    public double calculateBill(){
        double sum = 0;
        for(Meal meal : order){
            sum += meal.getPrice() * mealQuantities.get(meal.getName());
        }
        return sum;
    }

    // Clear the current order
    public void clearOrder() {
        order.clear();
        mealQuantities.clear();
        System.out.println("Order cleared.");
    }

    // Display the current order with meal quantities and total price
    public void displayOrder(){
        if (order.isEmpty()) {
            System.out.println("The menu is currently empty.");
            return;
        }

        int i = 1;
        for(String mealName : mealQuantities.keySet()){
            System.out.println(i + ". " + mealName + " x" + mealQuantities.get(mealName));
            i++;
        }
    }

    // Generate a formatted bill for the customer
    // customerName: The name of the customer
    // return String representation of the bill
    public String generateBill(String customerName) {
        String bill = new String();
        bill += ("\n===== Formatted bill =====\n");
        bill += ("Customer: " + customerName + "\n");
        bill += ("Order:\n");
        int i = 1;
        for(String mealName : mealQuantities.keySet()){
            bill += (i + ". " + mealName + " x" + mealQuantities.get(mealName) + "\n");
            i++;
        }
        bill += ("Total: RMB" + calculateBill() + "\n");
        bill += ("===================================\n");
        return bill;
    }
}